﻿// See https://aka.ms/new-console-template for more information
using System;
using System.Text;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            const string registryKeyPath = @"Software\miHoYo\崩坏：星穹铁道";

            if (args.Length == 0 || (args.Length == 1 && args[0] == "/?"))
            {
                Console.WriteLine("指定的参数错误，使用/?来输出帮助文档");
                Console.WriteLine("使用例: ");
                Console.WriteLine("  /CheckReg - 检查注册表是否存在");
                Console.WriteLine("  /FPS <value> - 设置FPS 30/60/120");
                Console.WriteLine("  /SetValue <value>  - 设置画质参数\n\n画质参数包括\nFPS\nEnableVSync\nRenderScale\nResolutionQuality\nShadowQuality\nLightQuality\nCharacterQuality\nEnvDetailQuality\nReflectionQuality\nBloomQuality\nAAMode");
                return;
            }

            switch (args[0])
            {
                case "/CheckReg":
                    CheckRegistryKey(registryKeyPath);
                    break;
                case "/Version":
                    Version();
                    break;
                case "/FPS":
                    if (args.Length > 1 && (args[1] == "30" || args[1] == "60" || args[1] == "120"))
                    {
                        SetFPSValue(registryKeyPath, "FPS", args[1]);
                    }
                    else
                    {
                        Console.WriteLine("FPS 必须为 30/60/120.");
                    }
                    break;
                case "/SetValue":
                    if (args.Length > 2)
                    {
                        SetFPSValue(registryKeyPath, args[1], args[2]);
                    }
                    else
                    {
                        Console.WriteLine("FPS 必须为 30/60/120.");
                    }
                    break;
                default:
                    Console.WriteLine("指定的参数错误，使用/?来输出帮助文档");
                    break;
            }
        }

        static String Version() 
        {
            return "1.0.0.1";
        }

        static int CheckRegistryKey(string keyPath)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath, true);

            if (key == null)
            {
                key.Close();
                return 0;
            }
            else
            {
                key.Close();
                return 1;
            }
        }

        static int SetFPSValue(string keyPath, string parameterName, string newValue)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(keyPath, true);

            if (key != null)
            {
                string valueName = "GraphicsSettings_Model_h2986158309";
                byte[] valueBytes = (byte[])key.GetValue(valueName);
                if (key == null || valueBytes == null)
                {
                    return 1;
                }
                string valueString = Encoding.UTF8.GetString(valueBytes);
                JObject jsonObject = JObject.Parse(valueString);

                if (jsonObject.ContainsKey(parameterName))
                {
                    // 更新参数值
                    if (parameterName == "FPS")
                    {
                        jsonObject[parameterName] = int.Parse(newValue);
                    }
                    else if (parameterName == "EnableVSync")
                    {
                        jsonObject[parameterName] = bool.Parse(newValue);
                    }
                    else if (parameterName == "RenderScale")
                    {
                        jsonObject[parameterName] = double.Parse(newValue);
                    }
                    else
                    {
                        jsonObject[parameterName] = int.Parse(newValue);
                    }

                    // 将更新后的JSON对象转换回字符串
                    string updatedJson = jsonObject.ToString(Newtonsoft.Json.Formatting.None);

                    // 更新注册表
                    byte[] updatedValueBytes = Encoding.UTF8.GetBytes(updatedJson);
                    key.SetValue(valueName, updatedValueBytes, RegistryValueKind.Binary);
                    key.Close();
                    return 0;
                }
                else
                {
                    Console.WriteLine($"参数 \"{parameterName}\" 不存在.");
                    return 1;
                }
            }
            else
            {
                Console.WriteLine($"注册表项 \"{keyPath}\" 不存在.");
                return 1;
            }
        }
    }
}